package com.mike.fragmentdemo3.Fragment.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.MyLocationStyle;
import com.mike.fragmentdemo3.R;

// 主活动类，继承自AppCompatActivity
public class MapActivity extends AppCompatActivity {
    // 定义一个请求码，用于权限请求的识别
    private static final int MY_LOCATION_REQUEST_CODE = 1001;
    // 声明MapView和AMap对象
    private MapView mMapView;
    private ImageView mBackIv;
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置活动的内容视图
        setContentView(R.layout.activity_main3);

        // 设置高德地图SDK隐私合规性
        MapsInitializer.updatePrivacyAgree(this, true);
        MapsInitializer.updatePrivacyShow(this, true, true);

        // 获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        // 在活动创建时，初始化地图
        mMapView.onCreate(savedInstanceState);

        // 检查是否已经授予了定位权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // 如果没有授予，请求定位权限
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_LOCATION_REQUEST_CODE);
        } else {
            // 如果权限已经被授予，初始化地图
            initMap();
        }
        mBackIv = findViewById(R.id.back_Iv);
        mBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MapActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    // 初始化地图的私有方法
    private void initMap() {
        // 如果aMap对象为空，则获取地图控制器对象
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        // 创建定位样式对象
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 设置定位间隔
        myLocationStyle.interval(2000);
        // 显示定位位置
        myLocationStyle.showMyLocation(true);
        // 设置定位样式
        aMap.setMyLocationStyle(myLocationStyle);
        // 启用定位按钮
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        // 设置定位类型为地图跟随模式
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
        // 启用地图定位功能
        aMap.setMyLocationEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在活动销毁时，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在活动恢复时，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 在活动暂停时，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // 在活动保存实例状态时，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 处理权限请求的结果
        if (requestCode == MY_LOCATION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 如果权限被授予，初始化地图
                initMap();
            } else {
                // 如果权限被拒绝，记录错误日志
                Log.e("LocationPermission", "Permission denied");
            }
        }

    }

    private void hideSystemUI() {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }


    //当窗口获得焦点时隐藏系统 UI

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }
}
